//
//  IntroduceController.m
//  quickLook
//
//  Created by J on 15/9/9.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "IntroduceController.h"
#import "header.h"
#import "IntroCell.h"
#import "CustomView.h"
#import "Masonry.h"
#import "ThirdNaviSearchController.h"


@interface IntroduceController ()<UITableViewDelegate,UITableViewDataSource,UINavigationBarDelegate>
{
    
        NSNumber* _number;
        BOOL isLoad;
        
    CustomView* _tableheaderView;

}

@property(nonatomic,strong)UITableView* tableView;

@property(nonatomic,strong)NSMutableArray* dataArray;


@end

@implementation IntroduceController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self netState];
  
}


- (void)netState
{
    
    
    [self createUI];
    [self loadData];
    
}



- (void)loadData
{
//介绍页信息
    _dataArray=[NSMutableArray array];
    [DownLoad getIntroDataWithID:self.model.id andBlock:^(NSDictionary* obj, NSError * error) {
    
     

        NSDictionary* values=obj[@"data"];
        
        Model* model=[[Model alloc]init];

        [model setValuesForKeysWithDictionary:values];
  
        
        [_tableheaderView refreshIntroduceVCWithModel:model];
        
        
        self.model=model;
        
        NSArray* topics=values[@"topics"];
        
        [topics enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger idx, BOOL *stop) {
            
            Model* model=[[Model alloc]init];
            
            
            [model setValuesForKeysWithDictionary:dic];
            
            [_dataArray addObject:model];
            
            
        }];
        
        
        
        [_tableView reloadData];
        
        isLoad=NO;
        
    }];
    
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    
    _tableView.separatorColor=SEPARATORCOLOR;
    
    
    [self.navigationController setNavigationBarHidden:YES];
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    [self.navigationController setNavigationBarHidden:NO];
}


- (void)createUI
{
    
    self.automaticallyAdjustsScrollViewInsets=YES;

       
    
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    [self.view addSubview:_tableView];
    
    
    //设置tableview背景色
    self.tableView.backgroundColor=[UIColor whiteColor];

    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"IntroCell" bundle:nil] forCellReuseIdentifier:@"IntroCell"];
 

    
    
    _tableheaderView=[[NSBundle mainBundle]loadNibNamed:@"CustomView4" owner:nil options:nil][0];
    
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.contentInset=UIEdgeInsetsMake(150, 0, 0, 0);
    
    [_tableView insertSubview:_tableheaderView atIndex:0];
    
    _tableheaderView.contentMode=UIViewContentModeScaleAspectFill;
    _tableheaderView.introBackgroundImage.contentMode=UIViewContentModeScaleAspectFill;
    _tableheaderView.IntroAvatar_url.contentMode=UIViewContentModeScaleAspectFill;

    _tableheaderView.frame=CGRectMake(0, -150, self.view.frame.size.width, 150);
    
    
    
    
    
    //左侧导航跳转按钮
    
    
    UIButton* button=[UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame=CGRectMake(20, 20, 40, 40);
    [button setBackgroundImage:[UIImage imageNamed:@"naviBackButton.png"] forState:UIControlStateNormal];
    
    
    [button addTarget:self action:@selector(pop:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    
    [self.view bringSubviewToFront:button];
    
    

    
}
//pop

- (void)pop:(UIButton*)sender
{
    
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float y=scrollView.contentOffset.y;
    
    
    JZJLog(@"%f",y);
    if (y<-150) {
        
        CGRect frame= _tableheaderView.frame;
        
        frame.origin.y=y;
        
        
        frame.size.height=-y;
        
        _tableheaderView.frame=frame;
      
    }
    
    
}

#pragma mark --UITableViewDataSource



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
            return _dataArray.count;
        

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
        

    IntroCell* cell=[tableView dequeueReusableCellWithIdentifier:@"IntroCell"];
    
    if (!cell) {
        
        cell=[[NSBundle mainBundle]loadNibNamed:@"IntroCell" owner:nil options:nil][0];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        
}
    
    
    Model* model=_dataArray[indexPath.row];
    
    [cell refreshIntroduceCellWithModel:model];
    

    return cell;
    
    
    
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;

}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    ThirdNaviSearchController* VC=[[ThirdNaviSearchController alloc]init];
    Model* model=_dataArray[indexPath.row];
    
    VC.model=model;
    
    [self.navigationController pushViewController:VC animated:YES];
    
    


}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UITableViewHeaderFooterView* view=(UITableViewHeaderFooterView*)[tableView dequeueReusableCellWithIdentifier:@"UITableViewHeaderFooterView"];
    
    if (!view) {
        
        view=[[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"UITableViewHeaderFooterView"];
        UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(10, 13, 100, 20)];
        view.contentView.backgroundColor=[UIColor whiteColor];
        label.text=@"TA的专题";
        label.textColor=JZJLIGHTGRAY;
        label.font=[UIFont systemFontOfSize:14];
        [view.contentView addSubview:label];
        
    }
    
    
    
    return view;




}



@end
